<?php
	$writePost = "<?php
require_once(\"database.php\");
$%_SESSION[$%sessionDepth] == \"\";
session_unset();
$%bodyString = \"\";
if ($%_SERVER['REQUEST_METHOD']==\"POST\") {
	$%id = $%_POST['id'];\n
	if ($%id) {
$postDataVarsString
		$%SQLeditQuery = \"UPDATE $tableName SET \";	
		$%SQLeditQuery .= \" $editPostDataSQLString \"; 
		$%SQLeditQuery .= \" WHERE id = '$%id' \";
		$%SQLeditResult = $%db->query($%SQLeditQuery);
	}
	else {
";
if ($isSortable) {
	$writePost .= "".
"		$%SQLsortQuery = \"SELECT MAX(sort_order) AS sort_order FROM $tableName\";
		$%SQLsortResult = $%db->query($%SQLsortQuery);
		$%sortArray = $%db->fetch_array($%SQLsortResult);
		$%sort_order = htmlentities($%sortArray['sort_order']);
		$%sort_order++;\n\n";
}

$writePost .= "".preg_replace ('/\$sort_order\s+.*?;/si','',$postDataVarsString)."
		$%SQLnewQuery = \"INSERT INTO $tableName ($newNamesPostDataSQLString) VALUES ($newValuesPostDataSQLString)\";
		$%SQLnewResult = $%db->query($%SQLnewQuery);
	} //end if(id) else
} // end if (request method = POST)
";

	$sortFlag = $hasTitleField ? "ORDER BY title ASC" : "";
	if ($isSortable) { $sortFlag = "ORDER BY sort_order ASC"; }
	
	$writeHead = "";
	
	if ($isStatic) {
		$writeHead .= "
$"."SQLhasRowsQuery = \"SELECT * FROM $tableName\";
$"."SQLhasRowsResult = $"."db->query($"."SQLhasRowsQuery);
$"."numRows = mysql_num_rows($"."SQLhasRowsResult);
if ($"."numRows == 0 ) {
  $"."SQLnewQuery = \"INSERT INTO $tableName ($newNamesPostDataSQLString) VALUES ($newValuesPostDataSQLString)\";
  $"."SQLnewResult = $"."db->query($"."SQLnewQuery);
}
";
	
	}

	$writeHead .= "
$%SQLlistQuery = \"SELECT * FROM $tableName $sortFlag\";
$%SQLlistResult = $%db->query($%SQLlistQuery);
while ($%rowArray = $%db->fetch_array($%SQLlistResult)) {\n";

	$writeBody = "\n						
		$%bodyString .= \"
  <div class='listPageButtons'>
    <a class='editBtn positiveBtns' href='$tableName" . "_write.php?id=$%id'>EDIT</a>";

	if(!$isStatic) {
		$writeBody .= "\n						
      <div class='deleteBtn'>
	      <a class='negativeBtns'>DELETE</a>
	      <div class='deleteConfirmation' id='deleteConfirmation$%id'>
	        Do you really want to delete? <a class='deleteYes'>YES</a> // <a class='deleteNo'>NO</a>
	      </div>
	    </div>";
  }
  
  $bodyClass = $isStatic ? "staticBody" : "";
  
	$writeBody .= "						    
  </div>
</div>  <!--end .backListRecord id:$%id-->\n\";
}
?>
<html>
	<head>
		<title>".$displayNameArray[$tableName]." List</title>
		<?php require_once('io_listPage_css.php'); ?>
	</head>
<body class='$bodyClass'>";

  include('navigation.php');

	if ($isSortable) { $sortClass = "sortable"; }
	$writeBody .= "
	<div class='recordsList $sortClass'>
  <!-- <a class='newBtn positiveBtns' href='" . $tableName . "_write.php'>NEW RECORD</a> -->
	  <?php echo $%bodyString;";
	$writeFooter = " ?>";
	
	if (!$isStatic) {  $writeFooter .= "<a class='newBtn positiveBtns' href='" . $tableName . "_write.php'>NEW RECORD</a>"; }
	$writeFooter .= "\n
	</div>
	\n
	<!-- Start JS Includes -->
	<?php require_once('io_listPage_js.php'); ?>
	<script type='text/javascript'>
	var jsTableName = '$tableName';

	$(function() {
		jQuery.fileDelete = function(id,field) {
			$.ajax({
				url: \"ajax.php\",
				data: \"tablename=\"+jsTableName+\"&id=\"+id+\"&field=\"+field+\"&AJAXtask=deleteFile\",
				cache: false,
				complete: function(){
					//alert(\"tablename=\"+jsTableName+\"&id=\"+id+\"&field=\"+field+\"&AJAXtask=deleteFile\");
				}
			});
		};	

		jQuery.deleteAllFiles = function(id,field) {
			$fieldsForDeletionString		
		};		
	});
	</script>
	</body>
</html>";
	$pageSource = str_replace("%", "", $writePost) . str_replace("%", "", $writeHead) . str_replace("%", "", $fieldVarsString) .  $backListElements . str_replace("%", "", $writeBody) . str_replace("%", "", $writeFooter);
	//echo "<br/><br/> backlistelements:" . $backListElements;
	//echo "<br/><br/> createList pagesource = " . $pageSource;
	//echo "<h2>tableName = $tableName</h2> ";

?>